#ifndef __SDL_TEXT_HPP
#define __SDL_TEXT_HPP

#include <SDL.h>
#include <SDL_ttf.h>
#include <string>

// this class is a singleton
class sdlTextEngine
{
private:
    TTF_Font *defaultFont;

    // private ctor for singleton class
    sdlTextEngine()
    {
        TTF_Init();
        defaultFont = TTF_OpenFont("fonts/freesansbold.ttf", 16);
        //if defaultFont == NULL, then really bad error has occurred
    }

    sdlTextEngine(const sdlTextEngine&);             // prevents copy-construction
    sdlTextEngine& operator=( const sdlTextEngine&); // prevents assignment

public:

    ~sdlTextEngine()
    {
        if( defaultFont != NULL )
            TTF_CloseFont( defaultFont ); // make sure you close the fonts you use
        TTF_Quit();
    }

    static sdlTextEngine& getInstance()
    {
        static sdlTextEngine instance;
        return instance;
    }

    int renderTextToSurface( SDL_Surface *screen,
                             SDL_Rect     rect,
                             const char  *text,
                             TTF_Font    *font = NULL)
    {
        if( font == NULL )
            font = defaultFont;
        int result = 0;

        SDL_Color color = {255, 255, 255};
        SDL_Surface *textSurface = TTF_RenderText_Solid(font, text, color);

        if( textSurface != NULL )
        {
            SDL_BlitSurface( textSurface, NULL, screen, &rect );
            SDL_FreeSurface( textSurface );
            result = 1;
        }
        // else error
        return result;
    }
};

#endif //__SDL_TEXT_HPP
